// pages/backendtest/index.js
// wx.cloud.callFunction({
//   name: "router_user",
//   data: {
//     router_op: "router_user_register",
//     openid: "jjjjjjjjjjj",
//     photo: "jjjjjjjjjjj",
//     username: "jjjjjjjjjjj",
//     type: "jjjjjjjjjjj"
//   }
// })
//   .then(res => { console.log(res) })
//   .catch(console.error)
Page({
  data: {
    user_id: "",
    openid: ""
  },
  login() {
    wx.cloud.callFunction({
      name: "common_getOPENID"
    }).then(res => {
      this.setData({
        openid: res.result
      })
      return res.result;
    }).then(res => {
      wx.cloud.callFunction({
        name: "user_login",
        data: {
          openid: this.data.openid,
          photo: "event.photo",
          username: "event.username",
          type: "event.type"
        }
      }).then(res => {
        console.log(res.result);
        this.setData({
          user_id: res.result[0]._id
        })
      })
    })
  },
  addStudent() {
    wx.cloud.callFunction({
      name: "user_addStudent",
      data: {
        u_id: this.data.user_id,
        i_id: "e54110dd644a5301002b870a50f78567",
        name: "林俊杰999",
        sex: "男",
        height: "177",
        weight: "200",
        grade: "3",
        class: "2",
        special: "0",
        bust: "0",
        waist: "0",
        hip: "0",
        default: "1"
      }
    }).then(res => {
      console.log("add opeation");
      console.log(res.result);
    })
  },
  deleteStudent() {
    wx.cloud.callFunction({
      name: "user_deleteStudent",
      data: {
        student_id: "2cc84e26644a16d90a57d679014241f8"
      }
    }).then(res => {
      console.log(res.result.stats);
    })
  },
  updateStudent() {
    wx.cloud.callFunction({
      name: "user_updateStudent",
      data: {
        student_id: "fc8e6465644a18990a4bcbad6f482a02",
        u_id: "event.u_id33321333",
        i_id: "event.u_id111",
        name: "event.u_id1",
        sex: "event.u_id1",
        height: "event.u_id1",
        weight: "event.u_id1",
        grade: "event.u_id1",
        class: "event.u_id1",
        special: "event.u_id1",
        bust: "event.u_id1",
        waist: "event.u_id1",
        hip: "event.u_id1",
        default: "event.u_id1"
      }
    }).then(res => {
      console.log(res.result[0]);
    })
  },
  getAllStu() {
    wx.cloud.callFunction({
      name: "user_getAllStu",
      data: {
        user_id: "7fc34a8464550e14001314766b1db9ea"
      }
    }).then(res => {
      console.log(res.result);//全部学生数组
      for (var i = 0; i < res.result.length; i++) {
        wx.cloud.callFunction({
          name: "institution_search_withSid",
          data: {
            key: "_id",
            value: res.result[i].i_id,
            student_id: res.result[i]._id
          }
        }).then(res => {
          console.log(res.result);//每个学生对应的学校信息，格式：student_id:{_id:"",code:"",name:"",type:""}
          for (var i in res.result) {
            console.log(i);//学生id，作为学校的key
            console.log(res.result[i]);//该学生id，对应的学校的value，for example: {_id: "", code: "", name: "", type: ""},直接存缓存或全局，用学生id可直接读取到。
          }
        })
      }
    })
  },
  switchDefaultStu() {
    wx.cloud.callFunction({
      name: "user_switchDefaultStu",
      data: {
        user_id: "64ee944c644bbe07000067a00eff5f8c",
        defaule_student_id: "0122a58764551c0d0b1e62b55930c65c"
      }
    }).then(res => {
      wx.cloud.callFunction({
        name: "user_getAllStu",
        data: {
          user_id: "64ee944c644bbe07000067a00eff5f8c"
        }
      }).then(res => {
        console.log(res.result);//修改默认学生后，返回更新后的全部学生信息
      })
    })
  },
  searchSchool() {
    wx.cloud.callFunction({
      name: "institution_search",
      data: {
        key: "code",
        value: "00000001"
      }
    }).then(res => {
      console.log(res.result);
    })
  },
  getAllInstitution() {
    wx.cloud.callFunction({
      name: "user_getAllInstitution",
      data: {
        user_id: "b14626cd644a0269000e4bc6584d30f5"
      }
    }).then(res => {
      console.log(res.result);
    })
  },
  addGood() {
    wx.cloud.callFunction({
      name: "good_addGood",
      data: {
        institution_id: "e54110dd644a5301002b870a50f78567",
        price: "20",
        number: "100",
        name: "风衣2",
        title: "风衣",
        img: "url",
        description: "风衣",
        tag: "风衣",
        type: "0",
        status: "2",
      }
    }).then(res => {
      console.log(res.result);//添加商品的结果
      return res.result;
    }).then(res => {
      wx.cloud.callFunction({
        name: "good_addParameter",
        data: {
          good_id: res[0]._id,
          para_name: "尺寸",
          para_value: "120(身高110-120cm 体重20-24kg),130(身高120-130cm 体重25-29kg),140(身高130-140cm 体重30-34kg),150(身高140-150cm 体重35-40kg),160(身高150-160cm 体重45-50kg),165(身高160-164cm 体重50-54kg),170(身高165-170cm 体重55-64kg),175(身高170-175cm 体重60-69kg),180(身高175-180cm 体重70-75kg),190(身高180-190cm 体重75-85kg)"
        }
      }).then(res => {
        console.log(res.result);//添加商品参数的结果
      })
    })
  },
  getGoodListforInstitution() {
    wx.cloud.callFunction({
      name: "good_getGoodListforInstitution",
      data: {
        institution_id: "e54110dd644a5301002b870a50f78567"
      }
    }).then(res => {
      // console.log(res.result); //商品列表
      var goods = res.result;
      for (var i = 0; i < res.result.length; i++) {
        console.log(goods[i]); //商品
        wx.cloud.callFunction({
          name: "good_getParameterforGood",
          data: {
            good_id: res.result[i]._id
          }
        }).then(res => {
          console.log(res.result);//商品参数
        })
      }
    })
  },
  updateGood() {
    wx.cloud.callFunction({
      name: "good_updateGood",
      data: {
        good_id: "93e4b6a0644a99ae0a6e2f7e5b3206e6",
        i_id: "e54110dd644a5301002b870a50f78567",
        price: "999",
        number: "55",
        name: "衬衣",
        title: "衬衣",
        img: "uurl",
        description: "描述",
        tag: "衬衣",
        type: "0",
        status: "2"
      }
    }).then(res => {
      console.log(res.result);
    })
  },
  addCart() {
    console.log("add cart operation");
    wx.cloud.callFunction({
      name: "shop_addCart",
      data: {
        student_id: "d322a8a664566ac600100cf970c04817",
        good_id: "fc8e6465644bbec80a798e2c60e7bf9f",
        number: "1",
        parameter: "{'尺寸':'120(身高110-120cm 体重20-24kg)'}"
      }
    }).then(res => {
      console.log(res.result);
    })
  },
  updateCart() {
    wx.cloud.callFunction({
      name: "shop_updateCart",
      data: {
        cart_id: "f1749dcc64550be9000fe1836400e63e",
        student_id: "d9c7841e644bc51a001220de41d530a0",
        good_id: "567c365f644bbedc000995f517852c96",
        number: "99",
        parameter: "{'尺寸':'140(身高110-120cm 体重20-24kg)'}"
      }
    }).then(res => {
      console.log(res.result);
    })
  },
  deleteCart() {
    wx.cloud.callFunction({
      name: "shop_deleteCart",
      data: {
        cart_id: "fc8e6465644b955e0a770ad4568bb7c5"
      }
    }).then(res => {
      console.log(res.result);
    })
  },
  getUserCartforInstitution() {
    console.log("getUserCartforInstitution")
    wx.cloud.callFunction({
      name: "shop_getUserCartforInstitution",
      data: {
        user_id: "7fc34a8464550e14001314766b1db9ea",
        institution_id: "e54110dd644a5301002b870a50f78567"
      }
    }).then(res => {
      console.log(res.result);
    })
  },
  addAddress() {
    wx.cloud.callFunction({
      name: "user_addAddress",
      data: {
        user_id: "64ee944c644bbe07000067a00eff5f8c",
        content: "北京3",
        default: "0"
      }
    }).then(res => {
      console.log(res.result);
      return res.result;
    })
  },
  deleteAddress() {
    wx.cloud.callFunction({
      name: "user_deleteAddress",
      data: {
        address_id: "93e4b6a0645669f10b45bac17049d868"
      }
    }).then(res => {
      console.log(res.result);
      return res.result;
    })
  },
  selectTest(key, value) {
    wx.cloud.callFunction({
      name: "model_user",
      data: {
        op: "select",
        key: key,
        value: value
      }
    })
      .then(res => { console.log(res) })
      .catch(console.error)
  },
  select() {
    this.selectTest("_id", "abd2309e643d602700045a2361ea9f5a")
  },
  addTest(openid, photo, username, type) {
    wx.cloud.callFunction({
      name: "model_user",
      data: {
        op: "add",
        openid: openid,
        photo: photo,
        username: username,
        type: type
      }
    })
      .then(res => { console.log(res) })
      .catch(console.error)
  },
  add() {
    this.addTest(1, 2, 3, 0)
  },
  deleteTest(id) {
    wx.cloud.callFunction({
      name: "model_user",
      data: {
        op: "delete",
        id: id,
      }
    }).then(res => { console.log(res) }).catch(console.error)
  },
  delete() {
    this.deleteTest("da7a77c8643eb38d001b39805fcfbff7")
  },
  updateTest(id, openid, photo, username, type) {
    wx.cloud.callFunction({
      name: "model_user",
      data: {
        op: "update",
        id: id,
        openid: openid,
        photo: photo,
        username: username,
        type: type
      }
    }).then(res => console.log(res)).catch(console.error)
  },
  update() {
    this.updateTest("93e4b6a0643eb639092013bf2da00543", "1", "1", "1", "1")
  },

  pay() {
    wx.cloud.callFunction({
      name: 'pay',
      data: {
        body: '支付测试页面paytest',
        detail: '[{"_id":"a6a6d15a645a51ed00154a6728d306f9","s_id":"fc8e6465645a51cb0ba1d7917378164e","g_id":"670c96a56458f93f001b1dc52dfa7b1e","number":1,"parameter":"160(身高150-160cm 体重45-50kg)"},{"_id":"fc8e6465645a51fe0ba1de1c1772b533","s_id":"fc8e6465645a51cb0ba1d7917378164e","g_id":"670c96a56458f93f001b1dc52dfa7b1e","number":1,"parameter":"160(身高150-160cm 体重45-50kg)","price":"60","title":"上衣（夏）","description":"上衣（夏）","thumb":"https://6f70-ops-0gnuzw0q7bcf4c1a-1317824511.tcb.qcloud.la/cloudbase-cms/upload/2023-05-08/9dzq1psx1wq3xv4qwqdqt8puoe6h8ifj_.jpg?sign=853129cf525cef3ab55833b4d1a5674d&t=1683553250","quantity":1,"specInfo":"160(身高150-160cm 体重45-50kg)","stockQuantity":"9999"},{"_id":"0122a587645a51ff0ba99ebd5651071b","s_id":"fc8e6465645a51cb0ba1d7917378164e","g_id":"670c96a56458f93f001b1dc52dfa7b1e","number":1,"parameter":"160(身高150-160cm 体重45-50kg)","price":"60","title":"上衣（夏）","description":"上衣（夏）","thumb":"https://6f70-ops-0gnuzw0q7bcf4c1a-1317824511.tcb.qcloud.la/cloudbase-cms/upload/2023-05-08/9dzq1psx1wq3xv4qwqdqt8puoe6h8ifj_.jpg?sign=853129cf525cef3ab55833b4d1a5674d&t=1683553250","quantity":1,"specInfo":"160(身高150-160cm 体重45-50kg)","stockQuantity":"9999"},{"_id":"a3e273fb645a52040014b03e22ec4019","s_id":"fc8e6465645a51cb0ba1d7917378164e","g_id":"305f880b6458f9720011356a41bd5ddc","number":1,"parameter":"120(身高110-120cm 体重20-24kg)","price":"60","title":"裤子（夏）","description":"裤子（夏）","thumb":"https://6f70-ops-0gnuzw0q7bcf4c1a-1317824511.tcb.qcloud.la/cloudbase-cms/upload/2023-05-08/fkcs4g5kl7npyl2hctn2bqyjl8m9p29f_.jpg?sign=05b5cfa7d7084a408f9f18de42e7627a&t=1683553262","quantity":1,"specInfo":"120(身高110-120cm 体重20-24kg)","stockQuantity":"9999"},{"_id":"30530c9a645a52050000d1177b38bc29","s_id":"fc8e6465645a51cb0ba1d7917378164e","g_id":"305f880b6458f9720011356a41bd5ddc","number":1,"parameter":"120(身高110-120cm 体重20-24kg)","price":"60","title":"裤子（夏）","description":"裤子（夏）","thumb":"https://6f70-ops-0gnuzw0q7bcf4c1a-1317824511.tcb.qcloud.la/cloudbase-cms/upload/2023-05-08/fkcs4g5kl7npyl2hctn2bqyjl8m9p29f_.jpg?sign=05b5cfa7d7084a408f9f18de42e7627a&t=1683553262","quantity":1,"specInfo":"120(身高110-120cm 体重20-24kg)","stockQuantity":"9999"},{"_id":"fd942587645a520e00024b056050fd63","s_id":"fc8e6465645a51cb0ba1d7917378164e","g_id":"709fba976458fab100198d5700876db9","number":1,"parameter":"120(身高110-120cm 体重20-24kg)","price":"190","title":"冬款棉衣（男）","description":"冬款棉衣（男）","thumb":"https://6f70-ops-0gnuzw0q7bcf4c1a-1317824511.tcb.qcloud.la/cloudbase-cms/upload/2023-05-08/isvq0s4tpolgwpjzzq3466z27f0804ow_.jpg?sign=782971cc455066a963dc66d2a44456af&t=1683553154","quantity":1,"specInfo":"120(身高110-120cm 体重20-24kg)","stockQuantity":"9999"},{"_id":"69186ef1645af764000379f422276b52","s_id":"fc8e6465645a51cb0ba1d7917378164e","g_id":"670c96a56458f93f001b1dc52dfa7b1e","number":1,"parameter":"160(身高150-160cm 体重45-50kg)","price":"60","title":"上衣（夏）","description":"上衣（夏）","thumb":"https://6f70-ops-0gnuzw0q7bcf4c1a-1317824511.tcb.qcloud.la/cloudbase-cms/upload/2023-05-08/9dzq1psx1wq3xv4qwqdqt8puoe6h8ifj_.jpg?sign=853129cf525cef3ab55833b4d1a5674d&t=1683553250","quantity":1,"specInfo":"160(身高150-160cm 体重45-50kg)","stockQuantity":"9999"},{"_id":"f60143d5645af76e00011244314f2979","s_id":"fc8e6465645a51cb0ba1d7917378164e","g_id":"305f880b6458f9720011356a41bd5ddc","number":1,"parameter":"160(身高150-160cm 体重45-50kg)","price":"60","title":"裤子（夏）","description":"裤子（夏）","thumb":"https://6f70-ops-0gnuzw0q7bcf4c1a-1317824511.tcb.qcloud.la/cloudbase-cms/upload/2023-05-08/fkcs4g5kl7npyl2hctn2bqyjl8m9p29f_.jpg?sign=05b5cfa7d7084a408f9f18de42e7627a&t=1683553262","quantity":1,"specInfo":"160(身高150-160cm 体重45-50kg)","stockQuantity":"9999"},{"_id":"f60143d5645af832000143686eed6a7a","s_id":"fc8e6465645a51cb0ba1d7917378164e","g_id":"709fba976458fab100198d5700876db9","number":1,"parameter":"160(身高150-160cm 体重45-50kg)","price":"190","title":"冬款棉衣（男）","description":"冬款棉衣（男）","thumb":"https://6f70-ops-0gnuzw0q7bcf4c1a-1317824511.tcb.qcloud.la/cloudbase-cms/upload/2023-05-08/isvq0s4tpolgwpjzzq3466z27f0804ow_.jpg?sign=782971cc455066a963dc66d2a44456af&t=1683553154","quantity":1,"specInfo":"160(身高150-160cm 体重45-50kg)","stockQuantity":"9999"}]',
        attach: '',
        totalFee: 1
      },
      success: res => {
        const payment = res.result.payment
        console.log(res)
        wx.requestPayment({//调用支付
          ...payment,
          success(res) {
            console.log('pay success', res)
          },
          fail(err) {
            console.error('pay fail', err)
          }
        })
      },
      fail: console.error,
    })
  },
  pay1() {
    wx.cloud.callFunction({
      name: 'pay',
      data: {
        body: '支付测试页面',
        detail: '支付一分钱测试支付',
        attach: '',
        //totalFee: 1
      }
    }).then((resp) => {
      console.log(resp);
    }).catch((e) => {
      console.log(e);
    });
  },
  queryAllOrder() {
    wx.cloud.callFunction({
      name: 'db_Order',
      config: {
        env: this.data.envId
      },
      data: {
        type: 'queryAllOrder'
      }
    }).then((resp) => {
      console.log(resp);
    }).catch((e) => {
      console.log(e);
    });
  },
  queryCloseOrder() {
    wx.cloud.callFunction({
      name: 'db_Order',
      config: {
        env: this.data.envId
      },
      data: {
        type: 'queryCloseOrder'
      }
    }).then((resp) => {
      console.log(resp);
    }).catch((e) => {
      console.log(e);
    });
  },
  queryOrderByOutTradeNo() {
    wx.cloud.callFunction({
      name: 'db_Order',
      config: {
        env: this.data.envId
      },
      data: {
        type: 'queryOrderByOutTradeNo',
        outTradeNo: 'CHFS1683396547zntdscpj7l',
      }
    }).then((resp) => {
      console.log(resp);
    }).catch((e) => {
      console.log(e);
    });
  },
  queryPaidOrder() {
    wx.cloud.callFunction({
      name: 'db_Order',
      config: {
        env: this.data.envId
      },
      data: {
        type: 'queryPaidOrder'
      }
    }).then((resp) => {
      console.log(resp);
    }).catch((e) => {
      console.log(e);
    });
  },
  queryPendingOeder() {
    wx.cloud.callFunction({
      name: 'db_Order',
      config: {
        env: this.data.envId
      },
      data: {
        type: 'queryPendingOeder'
      }
    }).then((resp) => {
      console.log(resp);
    }).catch((e) => {
      console.log(e);
    });
  },
  queryRefundOrder() {
    wx.cloud.callFunction({
      name: 'db_Order',
      config: {
        env: this.data.envId
      },
      data: {
        type: 'queryRefundOrder'
      }
    }).then((resp) => {
      console.log(resp);
    }).catch((e) => {
      console.log(e);
    });
  },
  updateOrderStatus() {
    wx.cloud.callFunction({
      name: 'db_Order',
      config: {
        env: this.data.envId
      },
      data: {
        type: 'updateOrderStatus',
        outTradeNo: 'CHFS1683643203grnsvg9q4tv',
        status: 'PAYERROR'
      }
    }).then((resp) => {
      console.log(resp);
    });
  },
  refund_pay() {
    wx.cloud.callFunction({
      name: 'refund_pay',
      data: {
        outTradeNo: 'CHFS16837810170jhl0mowgrcj',
        totalFee: 120000,
        refundFee: 120000,
        refundDesc: '测试退款',
        detail: '退款商品信息'
      }
    }).then((resp) => {
      console.log(resp);
    }).catch((e) => {
      console.log(e);
    });
  },
  queryServerOrder() {
    wx.cloud.callFunction({
      name: 'server_Order',
      data: {
        type: 'queryOrder',
        outTradeNo: 'CHFS1683810725tsfdu9hmc1n',
      }
    }).then((resp) => {
      console.log(resp);
    }).catch((e) => {
      console.log(e);
    });
  },
  closeServerOrder() {
    wx.cloud.callFunction({
      name: 'server_Order',
      data: {
        type: 'closeOrder',
        outTradeNo: 'CHFS16835599540txsuetocr5i',
      }
    }).then((resp) => {
      console.log(resp);
    }).catch((e) => {
      console.log(e);
    });
  },
  gopay() {
    const payment = { "appId": "wx765b53ecf8a47279", "timeStamp": "1683660821", "nonceStr": "iG8G0pi4DDX6zYbR", "package": "prepay_id=wx1003334149081184c16d8c9017fc540000", "signType": "MD5", "paySign": "B146BB1256A4CAB77DBEDA69E8CB151D" }
    console.log(payment)
    wx.requestPayment({//调用支付
      ...payment,
      success(res) {
        console.log('pay success', payment)
      },
      fail(err) {
        console.error('pay fail', payment)
      }
    })
  }

})